package com.whsxt.controller;

import com.github.pagehelper.PageInfo;
import com.whsxt.common.AjaxResult;
import com.whsxt.domain.Car;
import com.whsxt.dto.CarDto;
import com.whsxt.service.CarService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("/car")
@RequiresRoles(value = {"business", "admin", "user"}, logical = Logical.OR)
public class CarController {

    @Autowired
    private CarService carService;

    /**
     * 获取分页数据
     * @param carDto
     * @return
     */
    @RequestMapping("/findByPage.do")
    public AjaxResult findByPage(CarDto carDto){
        PageInfo<Car> pageInfo = carService.findByPage(carDto);
        return AjaxResult.ok(pageInfo);
    }

    /**
     * 添加车辆数据
     * @param carDto
     * @return
     */
    @RequestMapping("/add.do")
    @RequiresPermissions(value = {"car:add"})
    public AjaxResult add(CarDto carDto){
        boolean isOk = carService.save(carDto);
        return isOk ? AjaxResult.ok() : AjaxResult.error("添加车辆数据失败");
    }

    /**
     * 删除车辆数据
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @RequiresPermissions(value = {"car:delete"})
    public AjaxResult delete(Integer[] id){
        boolean isOk = carService.batchRemove(Arrays.asList(id));
        return isOk? AjaxResult.ok() : AjaxResult.error("删除数据失败");
    }

    @RequestMapping("/update.do")
    @RequiresPermissions(value = {"car:update"})
    public AjaxResult update(CarDto carDto){
        boolean isOk = carService.update(carDto);
        return isOk? AjaxResult.ok() : AjaxResult.error("修改车辆数据失败");
    }

    /**
     * 车辆出租接口
     * @return
     */
    @RequestMapping("/doRent.do")
    public AjaxResult doRent(Integer carId,  Integer customerId, String beginTime, String endTime){
        boolean isOk = carService.rent(carId, customerId, beginTime, endTime);
        return isOk ? AjaxResult.ok() : AjaxResult.error("租赁车辆失败");
    }
}
